package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbDistrictsBo;
import org.dromara.system.domain.vo.WbDistrictsVo;
import org.dromara.system.domain.WbDistricts;
import org.dromara.system.mapper.WbDistrictsMapper;
import org.dromara.system.service.IWbDistrictsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 县区Service业务层处理
 *
 * @author Lion Li
 * @date 2024-05-28
 */
@RequiredArgsConstructor
@Service
public class WbDistrictsServiceImpl implements IWbDistrictsService {

    private final WbDistrictsMapper baseMapper;

    /**
     * 查询县区
     */
    @Override
    public WbDistrictsVo queryById(Long districtId){
        return baseMapper.selectVoById(districtId);
    }

    /**
     * 查询县区列表
     */
    @Override
    public TableDataInfo<WbDistrictsVo> queryPageList(WbDistrictsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbDistricts> lqw = buildQueryWrapper(bo);
        Page<WbDistrictsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询县区列表
     */
    @Override
    public List<WbDistrictsVo> queryList(WbDistrictsBo bo) {
        LambdaQueryWrapper<WbDistricts> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbDistricts> buildQueryWrapper(WbDistrictsBo bo) {
        LambdaQueryWrapper<WbDistricts> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDistrictName()), WbDistricts::getDistrictName, bo.getDistrictName());
        lqw.eq(bo.getCityId() != null, WbDistricts::getCityId, bo.getCityId());
        return lqw;
    }

    /**
     * 新增县区
     */
    @Override
    public Boolean insertByBo(WbDistrictsBo bo) {
        WbDistricts add = MapstructUtils.convert(bo, WbDistricts.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDistrictId(add.getDistrictId());
        }
        return flag;
    }

    /**
     * 修改县区
     */
    @Override
    public Boolean updateByBo(WbDistrictsBo bo) {
        WbDistricts update = MapstructUtils.convert(bo, WbDistricts.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbDistricts entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除县区
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
